 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { getRoleConfig } from "utils/constants/bodyconstant";
import { priority } from "utils/constants/definetypes";
import { PositionHelper } from "utils/position/PositionHelper";
import { UtilsHelper } from "utils/position/UtilsHelper";
import { generateUniqueId } from "utils/utils";
 

/**
 * 侦察决策,定时发布侦察任务,目前就开始触发用来自动查找外矿吧
 */
export class SoutDecision< T extends "creep"|"room"="creep"|"room"> extends ABaseDecision<T>
{
   
    public isExpired(event: ObserverEvent, region: string): boolean 
    {
         
        const room = Game.rooms[event.region];
        
        const task1 = room.getTaskByType("room",(e:ITaskDeclare["room"])=>e.data.type == "observe");
        if(task1[0])
        {
           //  global.Log.Info(`存在Observe侦察任务 ${JSON.stringify(task1)}`);
            return false;
        }
        const task = room.getTaskByType("creep",(e:ITaskDeclare["creep"])=>e.data.type == "scout");
        if(task[0])
        {
            if(room.controller?.level ==8)
            {
                room.deleteTaskById(task[0].id,'creep');
            }
            else 
            {
                global.Log.Info(`存在侦察任务 ${JSON.stringify(task)}`);
                return false;
            }
            
        }
        return true;
    }
    public makeTask(event: ObserverEvent, region: string): IDecisionCode<T>|false
    {
        const room = Game.rooms[event.region];
        const ob = room.getStructures(STRUCTURE_OBSERVER)[0];
        if(ob)
        {
            return this.makeObserveTask(event,region);
        }
        else 
        {
            return this.makeScoutTask(event,region);
        }
    }
    public makeScoutTask(event: ObserverEvent, region: string): IDecisionCode<T>|false
    {
        // const bodyconfig =  getRoleConfig("scout",1);
        // const room = Game.rooms[event.region];
        // Game.rooms[event.region].log('info',`发起侦察任务`);
        // find 
     
        // const aroundroom = PositionHelper.calNeighborRoom(event.region );
        const exits = Object.values(Game.map.describeExits(event.region ));
         
        const task:ICreepTaskData<"scout">={
            type:"scout",
            arg:{
                targets:exits,
                visits:[]
            },
            creeps:[],
            room:event.region,
            body:{},
            income:-1,
            cost:4,
            required:[
                [['scout'], 1,1] 
            ]
        }
        const code:IDecisionCode<T>={
            region:event.region,
            isunique:false,
            task:[{
                id:generateUniqueId(),
                priority:priority.B,
                ttl:5000,
                type:"creep",
                shard:0,
                data:task,
            } as any],
        }
        return code;
        // return false;
    }
    

    public makeObserveTask(event: ObserverEvent, region: string): IDecisionCode<T>|false
    {
        // const bodyconfig =  getRoleConfig("scout",1);
        // const room = Game.rooms[event.region];
        
        // find 
        let scoutRooms = PositionHelper.calNeighborRoom(event.region,6);
        
        if(event.OBType==1)
        {
            scoutRooms = scoutRooms.filter(e=>UtilsHelper.IsHillway(e)&& 
            (
               !Memory.rooms[e] 
               || !Memory.rooms[e].lst 
                ||  ( ( Game.time - Memory.rooms[e]!.lst! ) > 100)
               )
            );
        }
        else 
        {
            scoutRooms = scoutRooms.filter(e=>!Memory.rooms[e] 
                || !Memory.rooms[e].lst 
                 ||  ( ( Game.time - Memory.rooms[e]!.lst! ) > 100)
                );
        }
        // 100tick内侦察过的就不看了
       
        const task:IRoomTaskData<"observe">={
            type:"observe",
            arg:{
                rooms:scoutRooms,//scoutRooms,
                
            },
        }
        
        const code:IDecisionCode<T>={
            region:event.region,
            isunique:false,
            task:[{
                id:generateUniqueId(),
                priority:priority.B,
                ttl:200,
                type:"room",
                shard:0,
                data:task,
            } as any],
        }
        return code;
        // return false;
    }
    
}